/*
 * $Id: ArtistList.java 346 2008-04-14 09:55:23Z jasta $
 *
 * Copyright (C) 2008 Josh Guilfoyle <jasta@devtcg.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

package org.devtcg.five.music.activity;

import org.devtcg.five.music.R;
import org.devtcg.five.music.widget.AlphabetListView;
import org.devtcg.five.music.widget.AlphabetSectionedAdapter;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Debug;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ArtistList extends Activity
{
	private static final String TAG = "ArtistList";

	private static final String PROJECTION[] = {
		"_id", "name", "full_name", "photo"
	};
	
	private AlphabetListView mList;

	private Cursor mCursor;

    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.artist_list);

        Intent i = getIntent();
        if (i.getData() == null)
        	i.setData(Uri.parse("content://org.devtcg.five/media/music/artists"));

        if (i.getAction() == null)
        	i.setAction(Intent.VIEW_ACTION);

        mCursor = managedQuery(i.getData(), PROJECTION, null, null, "name COLLATE UNICODE ASC");
        
        if (mCursor == null || mCursor.count() == 0)
        {
        	Toast.makeText(this, "No data, please use Five Sync first",
        	  Toast.LENGTH_LONG).show();
        	finish();
        	return;
        }

		mList = (AlphabetListView)findViewById(R.id.artist_list);

        mList.setAdapterParameters
         (R.layout.artist_list_item, R.layout.alphabet_separator, mCursor,
          new String[] { "photo", "full_name" },
          new int[] { R.id.artist_photo, R.id.artist_name },
          mCursor.getColumnIndex("name"));
        
//        mList = (ListView)findViewById(android.R.id.list);
//
//        ListAdapter adapter = new AlphabetSectionedAdapter(this,
//          R.layout.artist_list_item, R.layout.alphabet_separator,
//          mCursor,
//          new String[] { "full_name" },
//          new int[] { R.id.artist_name },
//          mCursor.getColumnIndex("name")
//        );

//        mList.setAdapter(adapter);
        mList.getListView().setOnItemClickListener(mClickEvent);

        //Debug.startMethodTracing("/tmp/artist-list");
    }
    
    @Override
    protected void onStop()
    {
    	//Debug.stopMethodTracing();
    	
    	super.onStop();
    }
    
    private OnItemClickListener mClickEvent = new OnItemClickListener()
    {
		public void onItemClick(AdapterView adapter, View v, int pos, long id)
		{
			Intent i = getIntent();

			/* Clicked on a separator row. */
			if (id == -1)
				return;

			Uri chosen = i.getData().buildUpon()
			  .appendPath(String.valueOf(id))
			  .build();
			
			String action = i.getAction();
			
			Log.d(TAG, "uri=" + chosen.toString());
			Log.d(TAG, "action=" + action);

			if (action.equals(Intent.PICK_ACTION) == true ||
			    action.equals(Intent.GET_CONTENT_ACTION) == true)
			{
				setResult(RESULT_OK, chosen.toString());
				finish();
			}
			else
			{
				startActivity(new Intent(Intent.VIEW_ACTION, chosen, ArtistList.this, AlbumAndSongList.class));
			}
		}
    };    
}
